var m_CurrentPageButton = "#ButtonLogInPage";

function homeNavigationButtonClicked(i_WhereToNavigate, i_ButtonToAddClass)
{
       jQuery.ajax({ 
       data: "action="+i_WhereToNavigate, 
       url: 'servletHome', 
       timeout: 2000, 
       error: function() {
         console.log('Failed to send ajax');
         }, 
       success: function(r) { 
         var data = jQuery.parseJSON(r); 
         changeDivMainContainer(data);
         addOrRemoveClassmainInfo(i_ButtonToAddClass);
         addClassButtonNavigationClicked(i_ButtonToAddClass);
  }});
}


function addClassButtonNavigationClicked(i_ButtonToAddClass){
        if(i_ButtonToAddClass != m_CurrentPageButton && i_ButtonToAddClass != "#ButtonLogInPage")
        {
            $(m_CurrentPageButton).removeClass('ButtonNavigationClicked');
            $(m_CurrentPageButton).addClass('ButtonNavigationNotClicked');
            $(i_ButtonToAddClass).removeClass('ButtonNavigationNotClicked');
            $(i_ButtonToAddClass).addClass('ButtonNavigationClicked');
            m_CurrentPageButton = i_ButtonToAddClass;
            $('#LinkLogin').show();
        }
        else
        {
            if(i_ButtonToAddClass == "#ButtonLogInPage" && i_ButtonToAddClass != m_CurrentPageButton)
            {
                m_CurrentPageButton = i_ButtonToAddClass;
                $('#LinkLogin').hide();
            }
        }
    }

function addOrRemoveClassmainInfo(i_ButtonToAddClass){
    if((i_ButtonToAddClass != "#ButtonHandRanking") && (i_ButtonToAddClass != "#ButtonLogInPage"))
    {
        $('#DivMainContainer').addClass('mainInfo');
    }
    else //hand Rankings
    {
        $('#DivMainContainer').removeClass('mainInfo');
    }
}

function changeDivMainContainer(i_data){
    $('#DivMainContainer').fadeOut("medium",function(){ 
        $('#DivMainContainer').empty(); 
        $('#DivMainContainer').append(i_data.DivMainContainer).fadeIn("medium");});                     
 }

function showSignUpFields()
{
	changeDivSignup("<br/><br/><br/><h4>Loading...</h4>");
       jQuery.ajax({  
       url: "SignUp.jsp", 
       type : "POST",
       timeout: 2000, 
       error: function() {
         console.log('Failed to send ajax');
         }, 
       success: function(page) {
         changeDivSignup(page);
  }});	
}

function changeDivSignup(i_data){
    $('#DivSignup').fadeOut("medium",function(){ 
        $('#DivSignup').empty(); 
        $('#DivSignup').append(i_data).fadeIn("medium");});                     
 }

function checkLogInValidation()
{
	errorNoDisplay();
	if(($('#TextPlayerUserName').val() != "") && ($('#TextPlayerPassword').val() != ""))
		{
			jQuery.ajax({
				url: "registration",
		        data: "action=logInValidation&playerUsername=" + $('#TextPlayerUsername').val() + "&playerPassword=" + $('#TextPlayerPassword').val(),
		        type : "POST",
		        timeout: 2000, 
		        error: function() {
		        	console.log('Failed to send ajax');
		        },
		        success: function(data) {
		        	if(data.canContinue == "true")
		        	{
		        		$('#FormLogin').submit();		        		
		        	}
		        	else
		        	{
		        		logInEroorDisplay("true", "true", "<h4>Username / Password do not match!</h4>");
		        	}
		        }});	
		}
	else
	{
		logInEroorDisplay(($('#TextPlayerUsername').val() == "")?"true":"false", ($('#TextPlayerPassword').val() == "")?"true":"false", "<h4>Enter Your Information.</h4>");
	}
}

function errorNoDisplay()
{
	$('#ErrorPlayerUsername').fadeOut();
	$('#ErrorPlayerPassword').fadeOut();
	$('#LogInError').fadeOut();
	$('#LogInError').empty();
}

function logInEroorDisplay(i_PlayerUsername, i_PlayerPassword, i_Massage)
{
	if (i_PlayerUsername == "true")
	{
		$('#ErrorPlayerUsername').fadeIn();
	}
	if (i_PlayerPassword == "true")
		{
			$('#ErrorPlayerPassword').fadeIn();
		}
	$('#LogInError').append(i_Massage);
	$('#LogInError').slideDown("slow");
}
